import Vue from 'vue'
import App from './App'


import StatusBar from "components/header/StatusBar.vue"
import cuCustom from './components/header/cu_custom.vue'
import Loading from "components/Loading/Loading.vue"
import NewCards from 'components/newCards.vue'
import $store from 'common/store.js'

let dat = {
	Domain: "https://xy.icefxy.com", //请求的域名
	resDomain: "../../static/img/", //资源地址
	appid: "wx5af09f4acf5b5c48", //小程序的appid
}
//配置的引用
Vue.prototype.Reques = dat;
Vue.prototype.$store = $store;
//组件的引用
Vue.component('cu-custom', cuCustom);
Vue.component('StatusBar', StatusBar);
Vue.component('Loading', Loading);
Vue.component('NewCards', NewCards);
//关于小应用中的方法调用 外卖平台的调用方法开始
Vue.prototype.apiHost = 'https://xy.icefxy.com'
Vue.prototype.apiHost1 = 'https://wm.icefxy.com';
Vue.prototype.resDomain = 'https://xy.icefxy.com/static/app';


Vue.prototype.encodeUrl = (url) => {
	return encodeURI(url);
}

Vue.prototype.statusState = {
	is_share: 0,
	user_id: -1,
	area_id: null
}

Vue.prototype.apiRequest = function(url, param = '', isLowding = 0, method = 'post', header = '') {
	header = {
		'Content-Type': 'application/json'
	}
	let timeOut = null;
	if (isLowding == 1) {
		timeOut = setTimeout(() => {
			uni.showLoading({
				title: '加载中',
				mask: true
			});
		}, 1500);
	}

	let authorization = uni.getStorageSync('authorization');
	this.$set(header, 'authorization', authorization);
	var promise = new Promise((resolve, reject) => {
		uni.request({
			url: this.apiHost + url,
			data: param,
			method: method,
			header: header,
			timeout: 5000,
			success: function(res) {
				clearTimeout(timeOut);
				let repeat = uni.getStorageSync('repeatQues');
				
				if (isLowding == 1) {
					uni.hideLoading();
				}
				if (res.data.code != 200) {
					let setRepeat = () => {
						uni.setStorageSync('repeatQues', true);
						setTimeout(() => {
							uni.setStorageSync('repeatQues', false);
						}, 1000);
					}
					setRepeat();
					if (res.data.msg && isLowding == 1) {
						uni.showToast({
							title: res.data.msg,
							icon: 'none'
						})
					}
					//为避免重复执行 设置变量阻止
					if (res.data.code == 1011 && !repeat) {
						uni.redirectTo({
							url: '/pages/basic/position'
						});
					}
					if (res.data.code == 401 && !repeat) {
						Vue.prototype.loginStatus = false;
						uni.removeStorageSync("authorization");
						uni.removeStorageSync("islogin");
						let islogin = uni.getStorageSync("islogin");
						if (islogin) {
							uni.setStorageSync("islogin", false);
							return;
						}
						uni.showToast({
							title: res.data.msg,
							icon: 'none'
						})
						setTimeout(() => {
							uni.navigateTo({
								url: '/pages/user/login',
								animationType: "fade-in"
							})
						}, 1000)

						return
					}

				}
				resolve(res.data);
			},
			fail: function(e) {
				if (isLowding == 1) {
					clearTimeout(timeOut);
					uni.hideLoading()
				}
				reject("网络出错");
			},
			complete: function(r) {

				if (r.statusCode == 404) {
					console.log("404");
					reject("出错啦~，请稍后再试");
				}
			}


		});

	});
	return promise;
}
//关于小应用中的方法调用 外卖平台的调用方法结束
Vue.config.productionTip = false
App.mpType = 'app'

Vue.prototype.getInfo = () => {
	return new Promise((resolve, reject) => {
		uni.getSystemInfo({
			success: (e) => {

				let custom = wx.getMenuButtonBoundingClientRect();
				let dat = {
					screenHeight: e.screenHeight,
					custom: custom,
					screenWidth: e.screenWidth,
					pixelRatio: e.pixelRatio
				}
				resolve(dat);
			}
		});
	})
}

Vue.prototype.subscribe = (...res) => {
	let tmp = {
		'food': 'dyEY4ezyH80voong4a2LkGj6x2xVgbzTqDvxlHXpziM',
		'order': 'Y0RbZuDfyZwAAGdHKI34jrVFw6hGfE7suZ-FIvNwhYY'
	}
	let arr = [];
	res.forEach(item => {
		console.log("key : " + item);
		arr.push(tmp[item])
	})

	return new Promise((resolve, reject) => {
		uni.requestSubscribeMessage({
			tmplIds: arr,
			success: (res) => {
				resolve();

			}

		})
	})
}


const app = new Vue({
	...App
})
app.$mount()
